package com.kucai.yifaqapp.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.Pay_BANKLISTBEAN;
import com.kucai.yifaqapp.model.network.entity.res.Pay_BIndCardReslut;
import com.kucai.yifaqapp.util.EventBus.MessageEvent;
import com.kucai.yifaqapp.util.SYSOUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by admin on 2018/12/3.
 */

public class Z_BindBankCardActivity extends BaseActivitySimple {
    private Button dianji;
    private RelativeLayout back;
    private LinearLayout bindbegin;
    private TextView banknumber;
    private List<String> mdatas = new ArrayList<>();
    private Pay_BANKLISTBEAN payBanklistbean;
    private String bank_id;
    private Button conform;
    private EditText wangdian;
    private EditText kahao;
    LinearLayout skinbg;
    private OptionsPickerView pvOptions;

    @Override
    protected int setContentView() {
        return R.layout.bindbankcard;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bindbankcard);
        dianji = findViewById(R.id.dianji);
        back = findViewById(R.id.back);
        skinbg = findViewById(R.id.skinbg);
        bindbegin = findViewById(R.id.bindbegin);
        conform = findViewById(R.id.conform);
        wangdian = findViewById(R.id.wangdianname);
        kahao = findViewById(R.id.kahao);
        bindbegin.setVisibility(View.GONE);
        dianji.setVisibility(View.VISIBLE);
        banknumber = findViewById(R.id.banknumber);
        dianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindbegin.setVisibility(View.VISIBLE);
                dianji.setVisibility(View.GONE);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        conform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wangdian.getText().toString().isEmpty()) {
                    Toast.makeText(Z_BindBankCardActivity.this, "请输入网点", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (kahao.getText().toString().isEmpty()) {
                    Toast.makeText(Z_BindBankCardActivity.this, "请输入卡号", Toast.LENGTH_SHORT).show();
                    return;
                }
                //开始提交银行数据
                HttpJsonUtils.getHttpApiInstance()
                        .getBandingcard( HttpAdress.getuseAdress() + "api/m/user/bindBank",Info.user.token, Integer.parseInt(bank_id), kahao.getText().toString(), wangdian.getText().toString())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new CustomObserver<Pay_BIndCardReslut>(Z_BindBankCardActivity.this) {
                            @Override
                            protected void onCustomNext(Pay_BIndCardReslut resBanner) {
                                try {
                                    if (resBanner.getCode() == 0) {
                                        Toast.makeText(Z_BindBankCardActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
                                        EventBus.getDefault().post(new MessageEvent("200"));
                                        finish();
                                    } else {
                                        Toast.makeText(Z_BindBankCardActivity.this, resBanner.getMsg(), Toast.LENGTH_SHORT).show();
                                    }
                                } catch (Exception e) {
                                    Toast.makeText(Z_BindBankCardActivity.this, "异常错误，请重新操作", Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            protected void onSubscribeCustom(Disposable disposable) {
                            }

                            @Override
                            protected void onErrorCustom(Throwable e) {
                                SYSOUtils.sOut(e.toString());
                            }
                        });
            }
        });
        initData();
    }

    private void initData() {
        HttpJsonUtils.getHttpApiInstance()
                .getBankList(HttpAdress.getuseAdress() + "api/m/user/getBanks",Info.user.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<Pay_BANKLISTBEAN>(this) {
                    @Override
                    protected void onCustomNext(final Pay_BANKLISTBEAN resBanner) {
                        try {
                            if (resBanner.getCode() == 0) {
                                payBanklistbean = resBanner;
                                for (int i = 0; i < resBanner.getData().size(); i++) {
                                    mdatas.add(resBanner.getData().get(i).getName());
                                }
                                pvOptions = new OptionsPickerView.Builder(Z_BindBankCardActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                                    @Override
                                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                                        banknumber.setText(mdatas.get(options1) + "");
                                        bank_id = resBanner.getData().get(options1).getBank_id();
                                    }
                                })
                                        .setSubCalSize(20)//确定和取消文字大小
                                        .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                                        .setCancelColor(Color.BLUE)//取消按钮文字颜色
                                        .setTextColorCenter(Color.BLACK)//设置选中项的颜色
                                        .build();
                                pvOptions.setPicker(mdatas);
                                banknumber.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        pvOptions.show();
                                    }
                                });

                            } else {
                                Toast.makeText(Z_BindBankCardActivity.this, resBanner.getMsg(), Toast.LENGTH_LONG).show();
                            }
                        } catch (Exception e) {
                            Toast.makeText(Z_BindBankCardActivity.this, "异常错误，请重新操作", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }
}
